<?php
namespace Home\Action;
use Common\Model\userModel;
class LoginAction extends BaseAction
{
    public function _initialize()
    {
        parent::_initialize();
    }

    public function index(){

        if($this->visitor->is_login){
            $this->redirect(U('user/ucenter','',''));
        }	
        if(1 == C('yh_sms_status')){ $this->assign('sms_status',true);}
        $this->_config_seo(array(
            'title'=>'用户登录'
            )); 

        $this->display();
    }


public function findpwd(){
  if(1 == C('yh_sms_status')){ $this->assign('sms_status',true);}
   if(IS_POST){
            $code = I('code','','trim');
            $phone = I('phone','','trim');
            if(session('sms_code') != $code || session('sms_phone') != $phone){
                $this->ajaxReturn(0, '手机验证码输入不正确');
            }
            $password = I('password','','trim');
            $repassword = I('repassword','','trim');
            if($password != $repassword){
                $this->ajaxReturn(0, '两次密码输入不一致');
            }
            $mo = new userModel();
            $where = array(
                'phone'=>$phone
            );
            $res = $mo->where($where)->setField('password', md5($password));
            if($res !== false){
                return $this->ajaxReturn(1, '密码修改成功！', U('login/index'));
            }
            $this->ajaxReturn(0, '操作失败');
        }
     $this->_config_seo(array(
            'title'=>'找回密码'
            )); 
  
  $this->display();	
	
}

 public function register()
    {
        if($this->visitor->is_login){
            $this->redirect('user/ucenter');
        }
if(1 == C('yh_sms_status')){ $this->assign('sms_status',true);}
     $track_val=cookie('trackid'); 
     if(!empty($track_val)){
        	$track=unserialize($track_val);
        	$uid=$track['t'];
        	$this->assign('recode',$track['recode']);
        	if($uid>0){
        		$where=array(
        		'id'=>$uid
        		);
        	}
    	}
     
     if(IS_POST){
      $phone = trimall(I('phone','','htmlspecialchars'));
       $username = trimall(I('username','','htmlspecialchars'));
       $email = trimall(I('email','','htmlspecialchars'));
       $password =trimall(I('password','','htmlspecialchars'));
       $repassword = trimall(I('repassword','','htmlspecialchars'));
        $invocode = trimall(I('invocode','','htmlspecialchars'));
        $code = I('code','','trim');
		if(!is_numeric($phone)){
			 $this->ajaxReturn(0, '非法操作');
			}
			
      if(1 == C('yh_sms_status') && (session('sms_code') != $code || session('sms_phone') != $phone)){
           $this->ajaxReturn(0, '手机验证码输入不正确');
      	}    
       
        if($invocode || $track_val){
   
        	
        	if($invocode){
        	 $where=array(
		'invocode' => $invocode
        	);
        	}
        	
        	$mod=new userModel();
		$exist = $mod->field('id,fuid,guid')->where($where)->find();
        	if($exist){
        	  	$data['fuid']=$exist['id'];
        	  	$data['guid']=$exist['fuid']?$exist['fuid']:0;
        	}else{
        	$this->ajaxReturn(0, '邀请码不存在或者大小写有误！');	
        	}
      
        }
          

            if($password != $repassword){
                $this->ajaxReturn(0, '两次密码输入不一致');
            }

            $res = $this->visitor->register($username, $phone, $email, $password, $data);

            if($res){
                return $this->ajaxReturn(1, '注册成功', U('user/ucenter'));
            }

            $this->ajaxReturn(0, $this->visitor->error);
        }


      

        $this->_config_seo(array(
            'title'=>'用户注册'
            ));

        $this->display();
    }

    public function pwdcode()
    {
        if(IS_POST){
            $phone = I('phone','','trim');
            $tempid=I('tempid','','trim');
            $ac=I('ac','','trim');
            $mod=new userModel();
            $res=$mod->where(array('phone'=>$phone))->find();
            if('reg' == $ac && $res){
               return $this->ajaxReturn(0, '此手机号已经被占用');
            }
            
             if('findpass' == $ac && !$res){
               return $this->ajaxReturn(0, '此手机号不存在');
            }
            
            $code = rand(100000, 999999);
            $data=array(
            'phone'=>I('phone','','trim'),
            'code'=>$code,
//          'webname'=>trim(C('yh_site_name')),
            'temp_id'=>$tempid
            );
           $res= $this->send_sms($data);
           if($res){
                session('sms_code', $code);
                session('sms_phone', $phone);
                return $this->ajaxReturn(1, '验证码发送成功');
            }
            $this->ajaxReturn(0, '验证码发送失败');
       }
       }


    public function login(){

        if($this->visitor->is_login){

            $this->redirect(U('user/ucenter','',''));
        }	

        if(IS_POST){

       $username = trimall(I('username','','htmlspecialchars'));

        $password = trimall(I('password','','htmlspecialchars'));

            $remember = I('remember','trim', 0);
            $code = I('code','','trim');
			
         if($this->check_verify($code) === false){
          $this->ajaxReturn(0, '验证码错误');
			 
          }

            $res = $this->visitor->login($username, $password, $remember);

            if($res){
                $role=$this->visitor->get('webmaster');

           
// $callback = I('callback','trim');
                    if(empty($callback)){$callback=U('user/ucenter');}

                    if(strpos($callback, '/login') !== false){
                        $callback = U('user/ucenter');
                    }
                    return $this->ajaxReturn(1, '登录成功', $callback?$callback:U('user/ucenter'), array(
                        'id'=>$this->visitor->get('id'),
                        'nickname'=>$this->visitor->get('nickname'),
                        'role'=>$this->visitor->get('webmaster')
                        ));



            }

            $this->ajaxReturn(0, $this->visitor->error);
			

        }


    }


    public function logout()
    {
        $this->visitor->logout();
        redirect('/');
        exit();
        $callback = $_SERVER['HTTP_REFERER'];
        if(
            strpos($callback, '/login') !== false ||
            strpos($callback, '/user') !== false 
            ){
            $callback = C('yh_headerm_html');
    }

    redirect($callback);
}



}  